package cn.itsource.system.controller;




import cn.itsource.system.domain.Role;
import cn.itsource.system.query.RoleQuery;
import cn.itsource.system.service.IRoleService;
import cn.itsource.util.AjaxResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService service;

    /**
     * 新增或者修改
     * @param role  如何role的id为空就是新增 否则就是修改
     * @return
     */
    @PostMapping
    public AjaxResult save(@RequestBody Role role){
        try {
            service.save(role);
            return AjaxResult.me().setSuccess(true).setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }


    /**
     * 根据id进行删除
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.deleteByPrimaryKey(id);
            return AjaxResult.me().setSuccess(true).setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     *  分页查询
     * @param roleQuery
     * @return
     */
    @PatchMapping
    public AjaxResult pageInfo(@RequestBody RoleQuery roleQuery){
        try {
            return AjaxResult.me().setSuccess(true).setMessage("操作成功").setResultObj(service.pageInfo(roleQuery));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败");
        }
    }

    /**
     * 批量删除   由于批量删除 前端可以看到传过来的其实是一个一个对象 把这些对象装在一个集合里面接收
     * @param
     * @return
     */
    @PatchMapping("/patchDelete")
    public AjaxResult patchDel(@RequestBody List<Role> roles){
        try {
            service.deleteByCollection(roles);
            return  AjaxResult.me().setSuccess(true).setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败");
        }
    }




}

